/*
 * GuildFraktionInfo - list the reputation and the guild level of all members of a wow guild
 * Copyright (C) [2012 - 2013] [www.marantis.de]
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public 
 * License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later 
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if not, 
 * see <http://www.gnu.org/licenses/>.
 */
package de.marantis.app.wow.gfi.control;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import de.marantis.app.wow.gfi.beans.ConfigBean;
import de.marantis.app.wow.gfi.beans.GuildCharacterBean;
import de.marantis.app.wow.gfi.beans.ResultDataBean;
import de.marantis.app.wow.gfi.gui.Gildeneinstellungen;
import de.marantis.app.wow.gfi.gui.Hauptframe;
import de.marantis.app.wow.gfi.gui.UpdateFrame;
import de.marantis.app.wow.gfi.helper.CommonHelper;
import de.marantis.app.wow.gfi.helper.ReputationEnumTypeSerializer;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

import javax.swing.text.Utilities;

public class MainConsoleController extends MainController {

	public Hauptframe haupt;

	public UpdateFrame update;

	public Gildeneinstellungen gilde;

	public ArrayList<GuildCharacterBean> volleliste;

	public MainConsoleController(ConfigBean config)
			throws IllegalArgumentException {
		super(config);
	}

	public MainConsoleController(ConfigBean config, Hauptframe haupt,
			UpdateFrame update, Gildeneinstellungen gilde)
			throws IllegalArgumentException {
		super(config);
		this.haupt = haupt;
		this.update = update;
		this.gilde = gilde;
	}

	/**
	 * Start the scan
	 */
	@Override
	public void scanArmory() {
		this.scanArmoryThread();
	}

	@Override
	public void setErrorUrl(String link) {
	}

	@Override
	public void setStatus(String msg) {
	}

	@Override
	public void scanStartCheck() {
	}

	@Override
	public void scanStart() {
		// log.info("Scan start ...");
		update.jLabel_ausgabe.setText("Scan start...");
		// System.out.println("Scan start...");
	}

	@Override
	public void scanEnd(ArrayList<GuildCharacterBean> guildMembers) {
		// log.info("Scan finished.");
		// System.out.println("Scan end.");
		update.jLabel_ausgabe.setText("Scan end.");
		if (guildMembers == null) {
			return;
		}

		File jsonFileComplete = this.config.getJsonExportFile();
		if (jsonFileComplete != null) {
			this.exportJsonResult(guildMembers, jsonFileComplete);
		}
		volleliste = this.exportResult(guildMembers);
	}

	@Override
	protected void startReputationThread(GuildCharacterBean guildChar,
			int completeCount, int actualIdx, HtmlLoadTypeEnum type) {
		try {
			if (guildChar == null) {
				return;
			}

			ReputationConsoleThread aThread = new ReputationConsoleThread(this,
					guildChar, completeCount, actualIdx, type);
			Thread.yield();

			/*
			 * wait until this thread is finished
			 */
			while (true) {
				if (aThread == null) {
					break;
				} else if (aThread.getState().equals(Thread.State.TERMINATED)) {
					aThread = null;
					break;
				} else if (!aThread.isAlive()) {
					aThread.start();
				}
				// Thread.sleep(100);
			}

			// log.debug("Guild member analyzed: " + guildChar.getCharName());

		} catch (Exception e) {
			// log.error(e);
		}
	}

	@Override
	protected void startItemLevelThread(GuildCharacterBean guildChar,
			int completeCount, int actualIdx, HtmlLoadTypeEnum type) {
		try {
			if (guildChar == null) {
				return;
			}

			ItemLevelConsoleThread aThread = new ItemLevelConsoleThread(this,
					guildChar, completeCount, actualIdx, type);
			Thread.yield();

			/*
			 * wait until this thread is finished
			 */
			while (true) {
				if (aThread == null) {
					break;
				} else if (aThread.getState().equals(Thread.State.TERMINATED)) {
					aThread = null;
					break;
				} else if (!aThread.isAlive()) {
					aThread.start();
				}
				// Thread.sleep(100);
			}

			// log.debug("Item level analyzed: " + guildChar.getCharName());

		} catch (Exception e) {
			// log.error(e);
		}
	}

	@Override
	protected void fireGuildMemberPageLoad(int idx) {
		// log.debug("Lade " + idx + ". Mitgliederseite ...");
	}

	@Override
	protected void memberListLoaded(ArrayList<GuildCharacterBean> chars) {
		if (chars == null) {
			return;
		}

		int count = chars.size();
		// log.info("Mitglieder zu analysieren: " + count);
	}

	private void exportJsonResult(ArrayList<GuildCharacterBean> guildMembers,
			File jsonFile) {

		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder.registerTypeHierarchyAdapter(Enum.class,
				new ReputationEnumTypeSerializer());
		Gson gson = gsonBuilder.create();
		

		ResultDataBean result = new ResultDataBean();
		
		result.setGuildLevelCount(this.config.getGuildLevelCount());
		result.setGuildLevelNames(this.config.getGuildLevelNames());
		result.setGuildLevelFilter(this.config.getGuildLevelFilter());
		result.setGuildReputationFilter(this.config.getGuildReputationFilter());
		result.setGuildMembers(guildMembers);
		// ArrayList<GuildCharacterBean> guildMemberswithRank = new
		// ArrayList<GuildCharacterBean>();
		//
		// for(int i=0;i<result.getGuildMembers().size();i++){
		// if ((result.getGuildMembers().get(i).getGuildRank())==3){
		// guildMemberswithRank.add(result.getGuildMembers().get(i));
		// }
		// }
		//
		// Collections.sort(guildMemberswithRank,new SortInv());
		//
		// System.out.println(guildMemberswithRank.toString());

		JsonResultBean jsonBean = new JsonResultBean(result);
		String jsonText = gson.toJson(jsonBean);

		String jsonReturn = "gfiResult(" + jsonText + ")";

		CommonHelper.writeFileText(jsonFile, jsonText);
	}

	public ArrayList<GuildCharacterBean> exportResult(
			ArrayList<GuildCharacterBean> guildMembers) {

		ResultDataBean result = new ResultDataBean();
		result.setGuildLevelCount(this.config.getGuildLevelCount());
		result.setGuildLevelNames(this.config.getGuildLevelNames());
		result.setGuildLevelFilter(this.config.getGuildLevelFilter());
		result.setGuildReputationFilter(this.config.getGuildReputationFilter());
		result.setGuildMembers(guildMembers);
		// System.out.println(guildMembers.toString());
		ArrayList<GuildCharacterBean> guildMemberswithRank = new ArrayList<GuildCharacterBean>();
		if (gilde.jCheckBoxfilterdeaktivieren.isSelected()) {
			guildMemberswithRank = result.getGuildMembers();
		} else {

			if (gilde.jCheckBoxInnofilter.isSelected()) {

				for (int i = 0; i < result.getGuildMembers().size(); i++) {
					if ((result.getGuildMembers().get(i).getGuildRank())
							.equals("Gildenmeister")) {
						guildMemberswithRank.add(result.getGuildMembers()
								.get(i));
					}
					if ((result.getGuildMembers().get(i).getGuildRank())
							.equals("Offizier")) {
						guildMemberswithRank.add(result.getGuildMembers()
								.get(i));
					}
					if ((result.getGuildMembers().get(i).getGuildRank())
							.equals("Raidmember")) {
						guildMemberswithRank.add(result.getGuildMembers()
								.get(i));
					}
					if ((result.getGuildMembers().get(i).getGuildRank())
							.equals("Trial")) {
						guildMemberswithRank.add(result.getGuildMembers()
								.get(i));
					}
				}
			} else {
				// TODO Fehler zum Auslesen der Raenge

				for (int i = 0; i < result.getGuildMembers().size(); i++) {
					if (gilde.jcomboraenge.getSelectedIndex() == 0) {
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang1.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}

					} else if (gilde.jcomboraenge.getSelectedIndex() == 1) {
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang1.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						} else if ((result.getGuildMembers().get(i)
								.getGuildRank()).equals(gilde.jtextrang2
								.getText().toString().trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
					} else if (gilde.jcomboraenge.getSelectedIndex() == 2) {
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang1.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang2.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang3.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
					} else if (gilde.jcomboraenge.getSelectedIndex() == 3) {
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang1.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang2.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang3.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang4.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
					} else if (gilde.jcomboraenge.getSelectedIndex() == 4) {
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang1.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang2.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang3.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang4.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang5.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}

					} else if (gilde.jcomboraenge.getSelectedIndex() == 5) {
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang1.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang2.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang3.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang4.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang5.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang6.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}

					} else if (gilde.jcomboraenge.getSelectedIndex() == 6) {
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang1.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang2.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang3.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang4.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang5.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang6.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}

						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang7.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}

					} else if (gilde.jcomboraenge.getSelectedIndex() == 7) {
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang1.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang2.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang3.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang4.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang5.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang6.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}

						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang7.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang8.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}

					} else if (gilde.jcomboraenge.getSelectedIndex() == 8) {
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang1.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang2.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang3.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang4.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang5.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang6.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}

						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang7.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang8.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}

						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang9.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}

					} else if (gilde.jcomboraenge.getSelectedIndex() == 9) {
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang1.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang2.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang3.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang4.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang5.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang6.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}

						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang7.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang8.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang9.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}
						if ((result.getGuildMembers().get(i).getGuildRank())
								.equals(gilde.jtextrang10.getText().toString()
										.trim())) {
							guildMemberswithRank.add(result.getGuildMembers()
									.get(i));
						}

					}
				}
			}
		}

		// if ((result.getGuildMembers().get(i).getGuildRank())
		// .equals("Gildenmeister")) {
		// guildMemberswithRank.add(result.getGuildMembers().get(i));
		// }
		// if ((result.getGuildMembers().get(i).getGuildRank())
		// .equals("Offizier")) {
		// guildMemberswithRank.add(result.getGuildMembers().get(i));
		// }
		// if ((result.getGuildMembers().get(i).getGuildRank())
		// .equals("Raidmember")) {
		// guildMemberswithRank.add(result.getGuildMembers().get(i));
		// }
		// if ((result.getGuildMembers().get(i).getGuildRank())
		// .equals("Trial")) {
		// guildMemberswithRank.add(result.getGuildMembers().get(i));
		// }

		// System.out.println(result.getGuildMembers().toString());
		// System.out.println(guildMemberswithRank.toString());
		try {
			Collections.sort(guildMemberswithRank, new SortInv());
		} catch (Exception e) {
			e.printStackTrace();
		}
		// System.out.println(guildMemberswithRank.toString());

		return guildMemberswithRank;

	}

	public Hauptframe getHaupt() {
		return haupt;
	}

	public void setHaupt(Hauptframe haupt) {
		this.haupt = haupt;
	}

	public UpdateFrame getUpdate() {
		return update;
	}

	public void setUpdate(UpdateFrame update) {
		this.update = update;
	}

	public ArrayList<GuildCharacterBean> getVolleliste() {
		return volleliste;
	}

	public void setVolleliste(ArrayList<GuildCharacterBean> volleliste) {
		this.volleliste = volleliste;
	}

	class JsonResultBean {
		ResultDataBean gfiData;

		JsonResultBean(ResultDataBean gfiData) {
			this.gfiData = gfiData;
		}
	}

	public class SortInv implements Comparator<GuildCharacterBean> {

		@Override
		public int compare(GuildCharacterBean o1, GuildCharacterBean o2) {

			if (o1.getItemLevel().getAverageItemLvl() < o2.getItemLevel()
					.getAverageItemLvl()) {
				return -1;
			} else if (o1.getItemLevel().getAverageItemLvl() > o2
					.getItemLevel().getAverageItemLvl()) {
				return 1;
			} else {
				return 0;
			}

		}
	}

}
